<template>
  <div class="login-bg">
    <div class="login-container">
      <div class="login-top">
        <div class="login-top-logo">
          <img src="~@/assets/logo.png" alt />
          <div class="login-top-title">Hdmin Pro</div>
        </div>
        <div class="login-top-desc">Hdmin Pro 前端设计解决方案</div>
      </div>
      <div>
        <n-form
          ref="formRef"
          size="large"
          :model="formInline"
          :rules="rules"
          label-placement="left"
        >
          <n-form-item path="username">
            <n-input v-model:value="formInline.username" placeholder="请输入用户名">
              <template #prefix>
                <n-icon size="18" color="#808695">
                  <PersonOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>
          <n-form-item path="password">
            <n-input
              v-model:value="formInline.password"
              type="password"
              show-password-on="click"
              placeholder="请输入密码"
            >
              <template #prefix>
                <n-icon size="18" color="#808695">
                  <LockClosedOutline />
                </n-icon>
              </template>
            </n-input>
          </n-form-item>
          <n-form-item class="default-color">
            <div class="flex justify-between">
              <div class="flex-initial">
                <n-checkbox v-model:checked="autoLogin">自动登录</n-checkbox>
              </div>
              <div class="flex-initial order-last">
                <a href="javascript:">忘记密码</a>
              </div>
            </div>
          </n-form-item>
          <n-form-item>
            <n-button type="primary" @click="handleSubmit" size="large" :loading="loading" block>登录</n-button>
          </n-form-item>
        </n-form>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { useRoute, useRouter } from "vue-router";
import { PersonOutline, LockClosedOutline, LogoGithub, LogoFacebook } from '@vicons/ionicons5';
import { useMessage } from 'naive-ui';
import { useUserStore } from '@/store/modules/user';
import { ResultEnum } from "@/enums/httpEnum";

interface FormState {
  username: string;
  password: string;
}

const formRef = ref();
const message = useMessage();
const loading = ref(false);
const autoLogin = ref(true);

const formInline = reactive({
  username: 'admin',
  password: '123456',
});

const rules = {
  username: {
    required: true,
    message: '请输入用户名',
    trigger: 'blur',
  },
  password: {
    required: true,
    message: '请输入密码',
    trigger: 'blur',
  },
};

const userStore = useUserStore();

const router = useRouter();
const route = useRoute();

const handleSubmit = (e) => {
  e.preventDefault();
  formRef.value.validate(async (errors) => {
    if (!errors) {
      const { username, password } = formInline;
      message.loading('登录中...');
      loading.value = true;

      const params: FormState = {
        username,
        password,
      };

      const { code, message: msg } = await userStore.login(params);
      if (code == ResultEnum.SUCCESS) {
        // decodeURIComponent 对url进行解码
        const toPath = decodeURIComponent((route.query?.redirect || "/") as string);
        message.success("登录成功!");
        loading.value = false;
        router.replace(toPath).then(_ => {
          if (route.name == "login") {
            router.replace("/");
          }
        })
      } else {
        message.info(msg || "登录失败!");
      }
    } else {
      message.error("请填写完整信息，并且进行验证码校验")
    }
  });
};
</script>
<style lang="less" scoped>
.login-bg {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: auto;

  .login-container {
    flex: 1;
    padding: 32px 0;
    width: 384px;
    margin: 0 auto;

    .login-top {
      padding: 32px 0;
      text-align: center;

      .login-top-logo {
        display: flex;
        justify-content: center;
        align-items: center;

        img {
          width: 60px;
          height: 60px;
        }

        .login-top-title {
          font-size: 40px;
          margin-left: 10px;
        }
      }

      .login-top-desc {
        font-size: 14px;
        color: #808695;
        height: 40px;
        line-height: 40px;
      }
    }
    .default-color {
      color: #515a6e;

      .ant-checkbox-wrapper {
        color: #515a6e;
      }
    }
  }
}
@media (min-width: 768px) {
  .login-bg {
    background-image: url("../../assets/image/login.svg");
    background-repeat: no-repeat;
    background-position: 50%;
    background-size: 100%;
  }
}
</style>
